package com.base.taskreports.common.utlis;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

public class DateUtils {

    public final static String DATE_REGEX = "^\\d{4}-\\d{2}-\\d{2}$" ;

    public static void main(String[] args) throws ParseException {

        System.out.println(isStrDate(null));

    }

    //String 转换为 Date
    public static Date transformatDate(String date) throws ParseException {

        SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal=Calendar.getInstance();
        cal.setTime(sp.parse(date));

        return cal.getTime();
    }

    // 判断字符串是否是日期
    public static boolean isStrDate(String strDate){
        return Pattern.matches(DATE_REGEX, strDate) ;
    }

    //获取之前日期
    public static String previousDay(String date ,int i) throws ParseException {

        SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal=Calendar.getInstance();
        cal.setTime(sp.parse(date));
        cal.add(Calendar.DATE,i);
        Date d=cal.getTime();

        return sp.format(d);
    }

    //获取之前日期
    public static String previousDay(int i){

        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,i);
        Date d=cal.getTime();

        SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd");
        return sp.format(d);
    }

    //获取之前日期
    public static String previousDayNoOperator(int i){

        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,i);
        Date d=cal.getTime();

        SimpleDateFormat sp=new SimpleDateFormat("yyyyMMdd");
        return sp.format(d);
    }

    //获取之前日期
    public static String previousDayNum(int i){

        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,i);
        Date d=cal.getTime();

        SimpleDateFormat sp=new SimpleDateFormat("dd");
        return sp.format(d);
    }
}
